var ajaxBusy = false;
var focusId;
jQuery(function() {
	jsf.ajax.addOnEvent(statusUpdate);
	var compTitle = jQuery('#component_title').text();
	var correlationId = jQuery('#correlationId').val();
	jQuery('#navForm li a').each(
			function() {
				toggleUiStateActive(this, compTitle);
			}
	);
	jQuery('#headerForm button').each(
			function() {
				toggleUiStateActive(this, correlationId);
			}
	).mouseleave(function() {
		toggleUiStateActive(this, correlationId);
	});
	jQuery('#nav li a').hover(function() {
			jQuery(this).addClass("ui-state-hover");
		}, function() {
			jQuery(this).removeClass("ui-state-hover");
		}
	);
	jQuery(':input:not(input[type=hidden])','#mainForm').live('focus', function(){
		focusId = NovelFaces.jqId(this.id);
	}); 
});

function statusUpdate(data) {
	if (data.status == 'begin') {
		ajaxBusy = true;
		showOrHideAjaxLoading(true);
	} else {
		ajaxBusy = false;
		showOrHideAjaxLoading(false);
		if(data.status == 'success' && focusId){
			jQuery(focusId).focus();
			focusId = null;
		}
	}
}

function showOrHideAjaxLoading(showOrHide) {
	showOrHide ? jQuery("#mainForm").mask("Waiting...") : jQuery("#mainForm").unmask();
}

function toggleUiStateActive(element, value){
	$this = jQuery(element);
	$this.toggleClass('ui-state-active', $this.text() == value);
}

var DragAndDrop = (function($) {
	var dragSrcEl_ = null;

	return {
		handleDragStart : function(element, e) {
			e.dataTransfer.effectAllowed = 'move';
			e.dataTransfer.setData('text/html', element.innerHTML);

			dragSrcEl_ = element;

			// this/e.target is the source node.
			$(element).addClass('moving');
		},
		handleDragOver : function(element, e) {
			if (e.preventDefault) {
				e.preventDefault(); // Allows us to drop.
			}

			e.dataTransfer.dropEffect = 'move';

			return false;
		},
		handleDragEnter : function(element, e) {
			$(element).addClass('over');
			if (e.preventDefault) {
				e.preventDefault(); // Allows us to drop.
			}

			e.dataTransfer.dropEffect = 'move';

			return false;
		},
		handleDragLeave : function(element, e) {
			// this/e.target is previous target element.
			$(element).removeClass('over');
		},
		handleDrop : function(element, e) {
			// this/e.target is current target element.

			if (e.stopPropagation) {
				e.stopPropagation(); // stops the browser from redirecting.
			}
			$('#mainForm\\:src').val($('header', $(dragSrcEl_)).text());
			$('#mainForm\\:dest').val($('header', $(element)).text());
			// Don't do anything if we're dropping on the same column we're dragging.
			if (dragSrcEl_ != element) {
				dragSrcEl_.innerHTML = element.innerHTML;
				element.innerHTML = e.dataTransfer.getData('text/html');

			}

			return false;
		},
		handleDragEnd : function(element, e) {
			// this/e.target is the source node.
			jQuery('#columns .column').removeClass('over moving');
		},
		isDragable : function(panel) {
			return 'draggable' in document.createElement(panel);
		}
	};
}(jQuery));